﻿using System;
using LoD.Contracts;
using LoD.Server.Core.Interfaces;
using Microsoft.Xna.Framework;


namespace LoD.Server.Core {
    public abstract class Item : IItem, IEntity, IComparable<IItem> {
        private int _amount;

        protected Item(TileSet tileSet, int tileID) {
            this.CreateTime = DateTime.Now;
            this.DecayTime = new TimeSpan(0, 30, 0);
            this.Decays = true;
            this.Visible = true;
            this.Movable = true;
            this.Walkable = true;
            this.Slope = TileSlope.None;
            this.Layer = Layer.None;
            this.Serial = Guid.NewGuid();
            this.Name = "Unnamed";
            this.Light = LightType.None;
            this.Amount = 1;
            this.TileSet = tileSet;
            this.TileID = tileID;
            this.TileType = TileType.StandardTile;
        }

        protected Item(TileSet tileSet, int tileID, TileType tileType) : this(tileSet, tileID) { this.TileType = tileType; }

        protected Item(TileSet tileSet, int tileID, TileType tileType, WorldPoint3D location, Map map) : this(tileSet, tileID, tileType) {
            this.Map = map;
            this.Location = location;
            this.MoveToWorld(this.Location, this.Map);
        }

        #region IComparable<IItem> Members

        public int CompareTo(IItem other) { return this.CompareTo((IEntity)other); }

        #endregion

        #region IEntity Members

        public int CompareTo(IEntity other) {
            if (other == null) return -1;
            return this.Serial.CompareTo(other.Serial);
        }

        public int CompareTo(object other) {
            if (other == null || other is IEntity) return this.CompareTo((IEntity)other);
            throw new ArgumentException();
        }

        public Guid Serial { get; private set; }

        public WorldPoint3D Location { get; set; }

        public Map Map { get; set; }

        public void Delete() { }

        public void ProcessDelta() { }

        public int Z {
            get { return this.Location.Z; }
            set { this.Location = new WorldPoint3D(this.X, this.Y, value); }
        }

        public int X {
            get { return this.Location.X; }
            set { this.Location = new WorldPoint3D(value, this.Y, this.Z); }
        }

        public int Y {
            get { return this.Location.Y; }
            set { this.Location = new WorldPoint3D(this.X, value, this.Z); }
        }

        #endregion

        #region IItem Members

        public DateTime CreateTime { get; set; }
        public string CreateAccount { get; set; }
        public TimeSpan DecayTime { get; set; }
        public bool Decays { get; set; }
        public bool Stackable { get; set; }
        public bool Visible { get; set; }
        public bool Movable { get; set; }
        public bool Walkable { get; set; }
        public bool IsLockedDown { get; set; }
        public double Weight { get; set; }
        public Color Hue { get; set; }
        public TileSlope Slope { get; set; }
        public Layer Layer { get; set; }
        public int TileID { get; set; }
        public TileSet TileSet { get; set; }
        public TileType TileType { get; set; }
        public string Name { get; set; }
        public LightType Light { get; set; }
        public Direction Direction { get; set; }

        public int Amount {
            get { return this._amount; }
            set { if (value > 0 && value <= 50000) this._amount = value; }
        }

        public bool QuestItem { get; set; }

        public void MoveToWorld(WorldPoint3D location, Map map) { }

        #endregion
    }
}